package com.simen.android.websocket;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.NotYetConnectedException;


public class OtherActivity extends AppCompatActivity {
    
    private static final String TAG = "MainActivity";
    
    private static final char lf = 10; // 换行
    private static final char nl = 0; // 消息结尾的标记，一定要
    
    TextView message;
    
    WebSocketClient client;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        
        message = findViewById(R.id.message);
        
        findViewById(R.id.connect).setOnClickListener(v -> {
            try {
                client = new WebSocketClient(
                        new URI("ws://10.24.248.43:5566/web-im/web/channel"),
                        new Draft_6455()
                ) {
                    
                    @Override
                    public void onOpen(ServerHandshake serverHandshake) {
                        Log.i(TAG, "握手成功");
                    }
                    
                    @Override
                    public void onMessage(String msg) {
                        Log.i(TAG, "收到消息==========" + msg);
                        if (msg.equals("over")) {
                            client.close();
                        }
                    }
                    
                    @Override
                    public void onClose(int i, String s, boolean b) {
                        Log.i(TAG, "链接已关闭");
                    }
                    
                    @Override
                    public void onError(Exception e) {
                        e.printStackTrace();
                        Log.i(TAG, "发生错误已关闭");
                    }
                };
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            
            client.connect();
        });
        
        findViewById(R.id.disConnect).setOnClickListener(v -> {
            if (client != null) {
                //Code must be in range [1000,5000):
                client.close(1000, "bye!");
                //webSocket.cancel();
                client = null;
            }
        });
        
        findViewById(R.id.subscribe).setOnClickListener(v -> {
            if (client != null) {
                StringBuilder sb = new StringBuilder();
                
                sb.append("SUBSCRIBE").append(lf); // 请求的命令策略
                sb.append("id:sub-9").append(lf);
                sb.append("destination:/topic/greetings").append(lf).append(lf).append(nl);
                
                client.send(sb.toString());
            } else {
                Log.e(TAG, "webSocket is null");
            }
        });
        
        Button button = findViewById(R.id.hello);
        button.setOnClickListener(v -> {
            
            if (client != null) {
                try {
                    client.send("哈喽,连接一下啊");
                } catch (NotYetConnectedException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e(TAG, "webSocket is null");
            }
        });
        
        
    }
    
}
